package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UpdateUser
 */
@WebServlet("/UpdateUser")
public class UpdateUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String u = request.getParameter("username");
		String p = request.getParameter("password");
		String t = request.getParameter("usertype");
		String uf = request.getParameter("fname");
		String ul = request.getParameter("lname");
		String e = request.getParameter("email");
		
		if(u.equals(userValidate(u))){
			updateUser(u,p,t,uf,ul,e);
			RequestDispatcher rd = request.getRequestDispatcher("listadmin.jsp");
			rd.forward(request, response);
		}
	}	
		
	String userValidate(String u){
		String checkUser ="";		
		try {
		
			Class.forName("org.sqlite.JDBC");
			String url = "jdbc:sqlite:c:/Users/JayeM/workspace/MyWebProject/MyWebProject/WebContent/database/database.db";
			Connection con =  DriverManager.getConnection(url);
			
			String sql = "select username from tbluserinfo where username="+"'"+u+"'";
			PreparedStatement pstmt = con.prepareStatement(sql);
			ResultSet rlt = pstmt.executeQuery();
			checkUser = rlt.getString(1);	
			pstmt.close();
			con.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return checkUser;
		
	}
	
	
	boolean updateUser(String u,String p, String t, String uf, String ul, String e){
		boolean updateUser = false;
		try {

			Class.forName("org.sqlite.JDBC");
			String url = "jdbc:sqlite:c:/Users/JayeM/workspace/MyWebProject/MyWebProject/WebContent/database/database.db";
			Connection con =  DriverManager.getConnection(url);
			con.setAutoCommit(false);
			String sql = "UPDATE tbluserinfo set username=?, userpassword=?, usertype=?,  ufirstname=?, ulastname=?,uemail=? where "+
			"username=?;";
						
				PreparedStatement pstmt = con.prepareStatement(sql);
				pstmt.setString(1, u);
				pstmt.setString(2, p);
				pstmt.setString(3, t);
				pstmt.setString(4, uf);
				pstmt.setString(5, ul);
				pstmt.setString(6, e);
				pstmt.setString(7, u);
			
				int checksql = pstmt.executeUpdate();
				if(checksql==0){
					updateUser=false;
				}
				
				else{
				con.commit();
				}
				pstmt.close();
				
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		return updateUser;
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
